<?php
namespace Admin\Model;
use Think\Model;
class PayModel extends Model 
{
    public $tableName = 'pay';

    //获取已提现的提现记录
    public function num($id)
    {
        //var_dump(111);exit();
        $arr['user_id'] = $id;
        $arr['is_user'] = 1;
        $arr['apply'] = 1;
        $arr['isApply'] = 1;
        //$res = strpos($arr['user_id'], $b)
        $weightModel = M($this->tableName);
        $weightRes = $weightModel->where($arr)->select();

        $res = array();
        for ($i=0; $i < count($weightRes); $i++) { 
            //判断此记录是不是提现记录
            $resMoney = strstr($weightRes[$i]['money'], '-');
            if($resMoney)
            {
                $res[$i] = $weightRes[$i];
            }
        }
        return $res;
    }

    //获取未审核的提现记录
    public function numOne($id)
    {
        //var_dump(111);exit();
        $arr['user_id'] = $id;
        $arr['is_user'] = 1;
        $arr['apply'] = 0;
        $arr['isApply'] = 0;
        //$res = strpos($arr['user_id'], $b)
        $weightModel = M($this->tableName);
        $weightRes = $weightModel->where($arr)->select();

        $res = array();
        for ($i=0; $i < count($weightRes); $i++) { 
            //判断此记录是不是提现记录
            $resMoney = strstr($weightRes[$i]['money'], '-');
            if($resMoney)
            {
                $res[$i] = $weightRes[$i];
            }
        }
        return $res;
    }

    //获取审核不通过的提现记录
    public function numTwo($id)
    {
        //var_dump(111);exit();
        $arr['user_id'] = $id;
        $arr['is_user'] = 1;
        $arr['apply'] = 0;
        $arr['isApply'] = 1;
        //$res = strpos($arr['user_id'], $b)
        $weightModel = M($this->tableName);
        $weightRes = $weightModel->where($arr)->select();

        $res = array();
        for ($i=0; $i < count($weightRes); $i++) { 
            //判断此记录是不是提现记录
            $resMoney = strstr($weightRes[$i]['money'], '-');
            if($resMoney)
            {
                $res[$i] = $weightRes[$i];
            }
        }
        return $res;
    }


    //获取审核的提现记录-批量
    public function applyYesTime($startTime,$endTime)
    {
/*         $arr['apply'] = 0;
        $arr['isApply'] = 0;
		$arr['shijian'] >= $startTime;
		$arr['shijian'] < $endTime; */
		
		$arr = "apply =1 AND isApply =1 AND money<0 AND shijian >= '$startTime' AND shijian < '$endTime'";
        $weightModel = M($this->tableName);
        $weightRes = $weightModel->where($arr)->order("shijian desc")->select();

		$res=array();
		$i=0;
        foreach($weightRes as &$rows) { 
            $resMoney = intval($rows['money']);
            if($resMoney<0){
                if($rows['user_id']>0)
                {
                    $userModel = M('user');
                    $user['user_Id'] = $rows['user_id'];
                    $userRes = $userModel->where($user)->select();
                    $rows['phone'] = $userRes[0]['phone'];
                    $rows['idcard'] = $userRes[0]['idcard'];
                    $rows['card'] = $userRes[0]['card'];
                    $rows['banktype'] = $userRes[0]['banktype'];
                    $rows['name'] = $userRes[0]['name'];
					$rows['type'] = '会员';
                }else{
                    $userModel = M('shop');
                    $user['shop_id'] = $rows['shop_id'];
                    $userRes = $userModel->where($user)->select();
                    $rows['phone'] = $userRes[0]['phone'];
                    $rows['idcard'] = $userRes[0]['idcard'];
                    $rows['card'] = $userRes[0]['card'];
                    $rows['banktype'] = $userRes[0]['banktype'];
                    $rows['name'] = $userRes[0]['contacts'];
					$rows['type'] = '商家';
					
				}
                $res[$i] = $rows;
				$i++;
            }
        }
        return $res;
    }	
    //获取未审核的提现记录
    public function applyYes()
    {
		$arr = "apply =1 AND isApply =1 AND money<0";
        $weightModel = M($this->tableName);
        $weightRes = $weightModel->where($arr)->order("shijian desc")->select();
		$res=array();
		$i=0;
        foreach($weightRes as &$rows) { 
            $resMoney = intval($rows['money']);
            if($resMoney<0){
                if($rows['user_id']>0)
                {
                    $userModel = M('user');
                    $user['user_Id'] = $rows['user_id'];
                    $userRes = $userModel->where($user)->select();
                    $rows['phone'] = $userRes[0]['phone'];
                    $rows['idcard'] = $userRes[0]['idcard'];
                    $rows['card'] = $userRes[0]['card'];
                    $rows['banktype'] = $userRes[0]['banktype'];
                    $rows['name'] = $userRes[0]['name'];
					$rows['type'] = '会员';
                }else{
                    $userModel = M('shop');
                    $user['shop_id'] = $rows['shop_id'];
                    $userRes = $userModel->where($user)->select();
                    $rows['phone'] = $userRes[0]['phone'];
                    $rows['idcard'] = $userRes[0]['idcard'];
                    $rows['card'] = $userRes[0]['card'];
                    $rows['banktype'] = $userRes[0]['banktype'];
                    $rows['name'] = $userRes[0]['contacts'];
					$rows['type'] = '商家';
					
				}
                $res[$i] = $rows;
				$i++;
            }
        }
        return $res;
    }	
	
    //获取未审核的提现记录-批量
    public function applyTime($startTime,$endTime)
    {
/*         $arr['apply'] = 0;
        $arr['isApply'] = 0;
		$arr['shijian'] >= $startTime;
		$arr['shijian'] < $endTime; */
		
		$arr = "apply =0 AND isApply =0  AND shijian >= '$startTime' AND shijian < '$endTime'";
        $weightModel = M($this->tableName);
        $weightRes = $weightModel->where($arr)->order("shijian desc")->select();

		$res=array();
		$i=0;
        foreach($weightRes as &$rows) { 
            $resMoney = intval($rows['money']);
            if($resMoney<0){
                if($rows['user_id']>0)
                {
                    $userModel = M('user');
                    $user['user_Id'] = $rows['user_id'];
                    $userRes = $userModel->where($user)->select();
                    $rows['phone'] = $userRes[0]['phone'];
                    $rows['idcard'] = $userRes[0]['idcard'];
                    $rows['card'] = $userRes[0]['card'];
                    $rows['banktype'] = $userRes[0]['banktype'];
                    $rows['name'] = $userRes[0]['name'];
					$rows['type'] = '会员';
                }else{
                    $userModel = M('shop');
                    $user['shop_id'] = $rows['shop_id'];
                    $userRes = $userModel->where($user)->select();
                    $rows['phone'] = $userRes[0]['phone'];
                    $rows['idcard'] = $userRes[0]['idcard'];
                    $rows['card'] = $userRes[0]['card'];
                    $rows['banktype'] = $userRes[0]['banktype'];
                    $rows['name'] = $userRes[0]['contacts'];
					$rows['type'] = '商家';
					
				}
                $res[$i] = $rows;
				$i++;
            }
        }
        return $res;
    }	
	
	
	
    //获取未审核的提现记录
    public function apply()
    {
        $arr['apply'] = 0;
        $arr['isApply'] = 0;

        $weightModel = M($this->tableName);
        $weightRes = $weightModel->where($arr)->order("shijian desc")->select();
		$res=array();
		$i=0;
        foreach($weightRes as &$rows) { 
            $resMoney = intval($rows['money']);
            if($resMoney<0){
                if($rows['user_id']>0)
                {
                    $userModel = M('user');
                    $user['user_Id'] = $rows['user_id'];
                    $userRes = $userModel->where($user)->select();
                    $rows['phone'] = $userRes[0]['phone'];
                    $rows['idcard'] = $userRes[0]['idcard'];
                    $rows['card'] = $userRes[0]['card'];
                    $rows['banktype'] = $userRes[0]['banktype'];
                    $rows['name'] = $userRes[0]['name'];
					$rows['type'] = '会员';
                }else{
                    $userModel = M('shop');
                    $user['shop_id'] = $rows['shop_id'];
                    $userRes = $userModel->where($user)->select();
                    $rows['phone'] = $userRes[0]['phone'];
                    $rows['idcard'] = $userRes[0]['idcard'];
                    $rows['card'] = $userRes[0]['card'];
                    $rows['banktype'] = $userRes[0]['banktype'];
                    $rows['name'] = $userRes[0]['contacts'];
					$rows['type'] = '商家';
					
				}
                $res[$i] = $rows;
				$i++;
            }
        }
        return $res;
    }

    //获取未审核的提现记录--打印--失效
    public function apply_dayin()
    {
        $arr['apply'] = 0;
        $arr['isApply'] = 0;

        $weightModel = M($this->tableName);
        $weightRes = $weightModel->where($arr)->select();
		$res=array();
		$i=0;
        foreach($weightRes as &$rows) { 
            $resMoney = intval($rows['money']);
            if($resMoney<0){
                if($rows['user_id']>0)
                {
                    $userModel = M('user');
                    $user['user_Id'] = $rows['user_id'];
                    $userRes = $userModel->where($user)->select();
					$drows['name'] = $userRes[0]['name'];
                    $drows['phone'] = $userRes[0]['phone'];
                    $drows['idcard'] = $userRes[0]['idcard'];
                    $drows['card'] = $userRes[0]['card'];
                    $drows['banktype'] = $userRes[0]['banktype'];
                    $drows['money'] = $userRes[0]['money'];
                    $drows['shijian'] = $userRes[0]['shijian'];                    
					$drows['type'] = '会员';
                }else{
                    $userModel = M('shop');
                    $user['shop_id'] = $rows['shop_id'];
                    $userRes = $userModel->where($user)->select();
					$drows['name'] = $userRes[0]['name'];
                    $drows['phone'] = $userRes[0]['phone'];
                    $drows['idcard'] = $userRes[0]['idcard'];
                    $drows['card'] = $userRes[0]['card'];
                    $drows['banktype'] = $userRes[0]['banktype'];
                    $drows['money'] = $userRes[0]['money'];
                    $drows['shijian'] = $userRes[0]['shijian'];                    
					$drows['type'] = '商家';
					
				}
                $res[$i] = $drows;
				$i++;
            }
        }
        return $res;
    }	
	
	
	
    /*提现审核通过*/
    public function via($id)
    {
        $pay['id'] = $id['id'];
        $payModel     = M($this->tableName);

        $newPay['apply'] = 1;
        $newPay['isApply'] = 1;
        $shop_res      = $payModel->data($newPay)->where($pay)->save();
        return $shop_res;
    }

        /*提现审核不通过*/
    public function noVia($id)
    {
        $pay['id'] = $id['id'];
        $payModel     = M($this->tableName);

        $newPay['apply'] = 0;
        $newPay['isApply'] = 1;
        $res      = $payModel->data($newPay)->where($pay)->save();
        return $res;
    }


    //获取审核通过的提现记录
    public function applyPay()
    {
        $arr['apply'] = 1;
        $arr['isApply'] = 1;

        $weightModel = M($this->tableName);
        $weightRes = $weightModel->where($arr)->select();

        $res = array();
        for ($i=0; $i < count($weightRes); $i++) { 
            $resMoney = strstr($weightRes[$i]['money'], '-');
            if($resMoney)
            {
                if($weightRes[$i]['is_user'])
                {
                    $userModel = M('user');
                    $user['user_Id'] = $weightRes[$i]['user_id'];
                    $userRes = $userModel->where($user)->select();
                    $weightRes[$i]['phone'] = $userRes[0]['phone'];
                    $weightRes[$i]['idcard'] = $userRes[0]['idcard'];
                    $weightRes[$i]['card'] = $userRes[0]['card'];
                    $weightRes[$i]['banktype'] = $userRes[0]['banktype'];
                    $weightRes[$i]['name'] = $userRes[0]['name'];
                    $weightRes[$i]['type'] = '会员';
                }else
                {
                    $shopModel = M('shop');
                    $shop['shop_id'] = $weightRes[$i]['shop_id'];
                    $shopRes = $shopModel->where($user)->select();
                    
                    $weightRes[$i]['contacts'] = $shopRes[0]['contacts'];
                    $weightRes[$i]['phone'] = $shopRes[0]['phone'];
                    $weightRes[$i]['idcard'] = $shopRes[0]['idcard'];
                    $weightRes[$i]['card'] = $shopRes[0]['card'];
                    $weightRes[$i]['banktype'] = $shopRes[0]['banktype'];
                    $weightRes[$i]['name'] = $shopRes[0]['name'];
                    $weightRes[$i]['type'] = '商家';
                }
                $res[$i] = $weightRes[$i];
            }
        }
        //var_dump($res);exit();
        return $res;
    }

    //充值记录
    public function pay()
    {
        $arr['apply'] = 1;
        $arr['isApply'] = 1;

        $weightModel = M($this->tableName);
        $weightRes = $weightModel->where($arr)->select();

        $res = array();
        for ($i=0; $i < count($weightRes); $i++) { 
            $resMoney = strstr($weightRes[$i]['money'], '-');
            if(!$resMoney)
            {
                if($weightRes[$i]['is_user'])
                {
                    $userModel = M('user');
                    $user['user_Id'] = $weightRes[$i]['user_id'];
                    $userRes = $userModel->where($user)->select();
					
                    $weightRes[$i]['phone'] = $userRes[0]['phone'];
                    $weightRes[$i]['card'] = $userRes[0]['card'];
                    $weightRes[$i]['banktype'] = $userRes[0]['banktype'];
                    $weightRes[$i]['name'] = $userRes[0]['name'];
                    $weightRes[$i]['type'] = '会员';
                }else
                {
                    $shopModel = M('shop');
                    $shop['shop_id'] = $weightRes[$i]['shop_id'];
					
                    $shopRes = $shopModel->where($shop)->select();
					//var_dump($shopRes);exit;
                    $weightRes[$i]['contacts'] = $shopRes[0]['contacts'];
                    $weightRes[$i]['phone'] = $shopRes[0]['phone'];
                    $weightRes[$i]['card'] = $userRes[0]['card'];
                    $weightRes[$i]['banktype'] = $userRes[0]['banktype'];
                    $weightRes[$i]['name'] = $userRes[0]['name'];
                    $weightRes[$i]['type'] = '商家';
                }
                $res[$i] = $weightRes[$i];
            }
        }
        //var_dump($res);exit();
        return $res;
    }

    //提现申请
    public function cash($arr)
    {
        $userModel = M('user');

        $use['user_Id'] = $arr['user_id'];

        $res = $userModel->where($use)->select();

        if($res[0]['use_points'] < 100 || $arr['number'] > $res[0]['use_points'])
        {
            return 'noNum';
        }else
        {
            $pay['user_id'] = $arr['user_id'];
            $pay['money']  = -$arr['number'];
            $pay['is_user'] = 1;
            $pay['apply'] = 0;
            $pay['isApply'] = 0;
            $pay['shijian'] = date('Y-m-d H:i:s',time());

            $random = $this->random();
            $pay['id'] = $random;
            
            $payModel = M($this->tableName);
            $res_in = $payModel->data($pay)->add();

            if($res_in)
            {
                usePoints($pay['user_id']);
                return 'in';
            }else
            {
                return 'no';
            }
        }
    }

    public function random()
    {
        return date('YmdHis') . str_pad(mt_rand(1, 99999), 5, '0', STR_PAD_LEFT);
    }   
}